package com.eedi.framework.event.service;

import cn.hutool.core.collection.CollUtil;
import com.eedi.framework.event.controller.vo.SysEventConsumerCreateReq;
import com.eedi.framework.event.controller.vo.SysEventConsumerQueryReq;
import com.eedi.framework.event.controller.vo.SysEventConsumerResp;
import com.eedi.framework.event.controller.vo.SysEventObsOrgResp;
import com.eedi.framework.common.event.annotations.event.EediEventPoint;
import com.eedi.framework.common.event.enums.SysEventConsumeStatusEnum;
import com.eedi.framework.common.exception.util.PreconditionsUtils;
import com.eedi.framework.common.pojo.PageResult;
import com.eedi.framework.event.controller.vo.*;
import com.eedi.framework.event.convert.SysEventConsumerConvert;
import com.eedi.framework.event.dal.mapper.SysEventConsumerMapper;
import com.eedi.framework.event.dal.mapper.SysEventObsOrgMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;

import static com.eedi.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.eedi.framework.event.enums.SysEventErrorCodeConstants.SYS_EVENT_OBS_ORG_IS_NULL;

/**
 * 事件服务
 *
 * @author zybourn
 */
@Service
@Slf4j
public class SysEventServiceImpl implements SysEventService {

    @Resource
    SysEventObsOrgMapper sysEventObsOrgMapper;
    @Resource
    SysEventConsumerMapper sysEventConsumerMapper;


    @Override
    public List<SysEventObsOrgResp> getObsOrg(EediEventPoint point) {
        return sysEventObsOrgMapper.getByPoint(point);
    }

    @Transactional
    @Override
    public void createConsumer(String msg, String sysEventObsOrgId) {
        SysEventObsOrgResp obsOrgResp = sysEventObsOrgMapper.getById(sysEventObsOrgId);
        PreconditionsUtils.checkNotNull(obsOrgResp, SYS_EVENT_OBS_ORG_IS_NULL);
        SysEventConsumerCreateReq consumer = SysEventConsumerConvert.INSTANCE.convert(obsOrgResp);
        consumer.setSysEventConsumeStatusEnum(SysEventConsumeStatusEnum.CREATED);
        sysEventConsumerMapper.create(consumer);
    }

    @Transactional
    @Override
    public void createConsumer(Collection<SysEventConsumerCreateReq> req) {
        if (CollUtil.isEmpty(req)){
            return;
        }
        for (SysEventConsumerCreateReq r:req){
            r.setSysEventConsumeStatusEnum(SysEventConsumeStatusEnum.CREATED);
        }
        sysEventConsumerMapper.create(req);
    }

    @Override
    public List<SysEventConsumerResp> getConsumerOnCreated() {
        SysEventConsumerQueryReq req=new SysEventConsumerQueryReq();
        req.setSysEventConsumeStatus(SysEventConsumeStatusEnum.CREATED);
        PageResult<SysEventConsumerResp> consumer = sysEventConsumerMapper.getConsumer(req);
        if (CollUtil.isNotEmpty(consumer.getList())){
            return sysEventConsumerMapper.getConsumerOnCreated(
                    consumer.getList().stream().map(SysEventConsumerResp::getSysEventConsumerId).toList()
            );
        }
        return List.of();
    }

    @Override
    public void setStatusToHandle(String sysEventConsumerId) {
        sysEventConsumerMapper.updateStatusToHandle(sysEventConsumerId);
    }

    @Override
    public void setStatusToException(String sysEventConsumerId, String msg) {
        sysEventConsumerMapper.updateStatusToException(sysEventConsumerId,msg);
    }

    @Override
    public void setStatusToSubmitted(String sysEventConsumerId, String msg) {
        sysEventConsumerMapper.updateStatusToSubmitted(sysEventConsumerId,msg);
    }


}
